var express = require('express');
var router = express.Router();
var moment = require('moment');
var request = require('request');
var db = require('./../../utils/db.js');

/**
 * 项目流程-设计 控制器
 */
router.get('/listAll', function (req, res, next) {
    var momentDate = moment(new Date());
    var createTime = momentDate.format('YYYY-MM-DD HH:mm:ss');
    var createDate = momentDate.format('YYYY-MM-DD');
    var createById = req.session.loginedUser ? req.session.loginedUser.id : null;
    var projectId = req.session.projectId;
    var selectDesignSql = "select * from project_progress_design where project_base_id = " + projectId;
    db.query(selectDesignSql, function (errDes, rowsDes) {
        if (rowsDes && rowsDes.length > 0) {
            res.render('process/design/listAll', {
                designId: rowsDes[0].id,
                designProfile: rowsDes[0].design_content_profile,
                designDetailed: rowsDes[0].design_content_detailed
            });
        } else {
            var designSql = "insert into project_progress_design(design_content_profile,design_content_detailed,project_base_id,create_time,create_date,create_by_id,remark) values " +
                "(NULL,NULL," + projectId + ",'" + createTime + "','" + createDate + "'," + createById + ",'')";
            db.query(designSql, function (err, rowsdes) {
                if (!err) {
                    var profile;
                    var detailed;
                    var designId = rowsdes.insertId;
                    var selectDesFoundSql = "select * from project_progress_design where id = " + designId;
                    db.query(selectDesFoundSql, function (errDesFound, rowsDesFound) {
                        if (rowsDesFound && rowsDesFound.length > 0) {
                            profile = rowsDesFound[0].design_content_profile;
                            detailed = rowsDesFound[0].design_content_detailed;
                            res.render('process/design/listAll', {
                                designId: designId,
                                designProfile: profile,
                                designDetailed: detailed
                            });
                        } else {
                            res.json({
                                errcode: 1,
                                message: '搜索设计失败'
                            });
                        }
                    });
                } else {
                    res.json({
                        errcode: 1,
                        message: '创建设计失败'
                    });
                }
            });
        }
    })
});

router.post('/save', function (req, res, next) {
    var moduleId = req.body.uploadId;
    var designType = req.body.designType;
    var designId = req.body.designId;
    var profile = req.body.designProfile;
    var detailed = req.body.designDetailed;

    var updateSql = '';
    if (designType == 1) {
        if (!profile) {
            profile = moduleId;
        } else {
            profile += "," + moduleId;
        }
        updateSql = "update project_progress_design set design_content_profile='" + profile + "'  where id=" + designId;
        db.query(updateSql, function (err, rows) {
            if (!err) {
                // res.redirect("/process/design/listAll");
                res.json({
                    errcode: 0,
                    message: '保存成功',
                    data: {
                        designType: designType,
                        profile: profile,
                        detailed: detailed
                    }
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '保存失败'
                });
            }
        });
    } else if (designType == 2) {
        if (!detailed) {
            detailed = moduleId;
        } else {
            detailed += ";" + moduleId;
        }
        updateSql = "update project_progress_design set design_content_detailed='" + detailed + "'  where id=" + designId;
        db.query(updateSql, function (err, rows) {
            if (!err) {
                // res.redirect("/process/design/listAll");
                res.json({
                    errcode: 0,
                    message: '保存成功',
                    data: {
                        designType: designType,
                        profile: profile,
                        detailed: detailed
                    }
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '保存失败'
                });
            }
        });
    }
});


module.exports = router;